#include "Game.h"

Ball::Ball()
{
	m_Size = Vec2f(20,20);
	m_Pos = Vec2f(600,500);
	m_Vel=Vec2f(5,0);
	LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold paddle vertices
}

void Ball::Reflect()
{
	m_Vel.SetY(-m_Vel.Y());
}
void Ball::PaddleHit(float y)
{
	float newy=(m_Pos.Y()-y)/10;
	if (m_Vel.X()>0)
	{
		m_Vel.SetX(-(m_Vel.X()+0.5));
	}
	else
	{
		m_Vel.SetX(-(m_Vel.X()-0.5));
	}
	m_Vel.SetY(newy);

}
HRESULT Ball::Load()
{
	//Use D3DX to create a texture from file
	if(FAILED(D3DXCreateTextureFromFile(TheGame::Instance()->m_pd3dDevice, L"Ball.png", GetTexture())))
	{
		//If texture isn't in current folder, check parent folder
		if(FAILED(D3DXCreateTextureFromFile(TheGame::Instance()->m_pd3dDevice, L"..\\Ball.png", GetTexture())))
		{
			MessageBox(0, L"Could not find texture", L"Texture error", 0);
			return E_FAIL;
		}
	}
	return S_OK;
}
HRESULT Ball::Update()
{
	if (m_Pos.Y()<=0 || m_Pos.Y()>=1000)
	{
		Reflect();
	}
	m_Pos+=m_Vel;
	CUSTOMVERTEX paddle[] =
	{
		{ m_Pos.X()-(m_Size.X()/2),  m_Pos.Y()-(m_Size.Y()/2), 0.5f, 1.0f, 0, 0}, // x, y, z, rhw, color
		{ m_Pos.X()+(m_Size.X()/2), m_Pos.Y()-(m_Size.Y()/2), 0.5f, 1.0f, 1, 0},
		{  m_Pos.X()-(m_Size.X()/2), m_Pos.Y()+(m_Size.Y()/2), 0.5f, 1.0f, 0, 1},
		{  m_Pos.X()+(m_Size.X()/2), m_Pos.Y()+(m_Size.Y()/2), 0.5f, 1.0f, 1, 1},
	};

	//Update collision volume
	m_volume.left = (long)((-m_Size.X() / 2) + m_Pos.X());
	m_volume.top = (long)((-m_Size.Y() / 2) + m_Pos.Y());
	m_volume.right = (long)((m_Size.X() / 2) + m_Pos.X());
	m_volume.bottom = (long)((m_Size.Y() / 2) + m_Pos.Y());

	// Create the vertex buffer. Here we are allocating enough memory
	// (from the default pool) to hold all our 4 custom vertices. We also
	// specify the FVF, so the vertex buffer knows what data it contains.
	if( FAILED( TheGame::Instance()->m_pd3dDevice->CreateVertexBuffer( 4 * sizeof( CUSTOMVERTEX ),
		0, D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT, &m_pVB, NULL ) ) )
	{
		return E_FAIL;
	}

	// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
	// gain access to the vertices. This mechanism is required becuase vertex
	// buffers may be in device memory.
	VOID* pVertB;
	if( FAILED( m_pVB->Lock( 0, sizeof( paddle ), ( void** )&pVertB, 0 ) ) )
		return E_FAIL;
	memcpy( pVertB, paddle, sizeof( paddle ) );
	m_pVB->Unlock();

	return S_OK;

}
void Ball::Draw()
{
	//Enable Alpha-Testing
	TheGame::Instance()->m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); 
	TheGame::Instance()->m_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); 
	TheGame::Instance()->m_pd3dDevice->SetRenderState(D3DRS_ALPHAREF, (DWORD)8); 
	TheGame::Instance()->m_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); 

	//Draw Ball
	TheGame::Instance()->m_pd3dDevice->SetTexture(0, m_Texture);
	TheGame::Instance()->m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
	TheGame::Instance()->m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
	TheGame::Instance()->m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
	TheGame::Instance()->m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );

	TheGame::Instance()->m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof( CUSTOMVERTEX ) );
	TheGame::Instance()->m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
	TheGame::Instance()->m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
}